package com.mlink.ai.chat.ui.fragment

import android.app.Dialog
import android.os.Bundle
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.mlink.ai.chat.assistant.robot.R
import com.mlink.ai.chat.assistant.robot.databinding.FragmentSelectShareBinding
import com.mlink.ai.chat.utils.AnalyticsEvents
import com.mlink.ai.chat.utils.AnalyticsParams
import com.mlink.ai.chat.utils.dp2px
import com.mlink.ai.chat.utils.logEvent

class SelectShareBottomDialogFragment : BottomSheetDialogFragment() {

    private val binding: FragmentSelectShareBinding by lazy {
        FragmentSelectShareBinding.inflate(layoutInflater)
    }
    var listener: OnFunctionListener? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(DialogFragment.STYLE_NORMAL, R.style.CustomBottomSheetDialogTheme)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        initView()
        return binding.root
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return super.onCreateDialog(savedInstanceState).apply {
            setOnKeyListener { _, keyCode, keyEvent ->
                if (keyCode == KeyEvent.KEYCODE_BACK && keyEvent.action == KeyEvent.ACTION_UP) {
                    dismiss()
                    true
                } else {
                    false
                }
            }
        }
    }

    override fun onStart() {
        super.onStart()
        dialog?.run {

            val parent = view?.parent as? ViewGroup
            parent?.setBackgroundResource(R.color.transparent)

            view?.layoutParams?.height = 220.dp2px()
        }
    }

    private fun initView() {
        binding.rlCamera.setOnClickListener {
            AnalyticsEvents.AC_CHATAI_HISTORY_SHARE_SEL.logEvent(
                Bundle().apply {
                    putString(AnalyticsParams.KEY_TYPE, AnalyticsParams.SHARE_TYPE_TXT)
                }
            )
            listener?.onTxtClick()
        }

        binding.rlPhoto.setOnClickListener {
            AnalyticsEvents.AC_CHATAI_HISTORY_SHARE_SEL.logEvent(
                Bundle().apply {
                    putString(AnalyticsParams.KEY_TYPE, AnalyticsParams.SHARE_TYPE_PIC)
                }
            )
            listener?.onPhotoClick()
        }
    }


    interface OnFunctionListener {
        fun onTxtClick()
        fun onPhotoClick()
    }


}